<?php
/**
 * 《工作信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Equipment.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Plan extends EmpUtil {
	public $tbl = Constant::tbl_emp_plan;
	
	public $type = array(
		'-' => '请选择',
		'1' => '普通',
		'2' => '紧急',
		'3' => '重要',	

		'9' => '其它',
	);
	
	public $evaluation = array(
		'-' => '请选择',
		'2' => '差',
		'3' => '中',
		'4' => '良',
		'5' => '优',

		'0' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '已经审核',
			
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'date' => '日期',
		'name' => '事由',
			
		'status' => '状态',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '员工',
			
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('evaluation_option', $this->evaluation);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('pagesize_s', '50');

		$controller->display($request, 'emp/plan/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		
		$type_s = $request['type_s'];
		$evaluation_s = $request['evaluation_s'];

		$my = $this->_my();
		
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($name_s)) {
			$where .= " AND name like '%" . $name_s . "%'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($evaluation_s) && $evaluation_s <> '-') {
			$where .= " AND evaluation = '" . $evaluation_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/plan/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$summary = $request['summary'];
		$experience = $request['experience'];
		$name = $request['name'];
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, summary, experience, name, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $summary, $experience, $name, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/plan/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$summary = $request['summary'];
		$experience = $request['experience'];
		$name = $request['name'];
		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', summary = '%s', experience = '%s', name = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $summary, $experience, $name, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_files_clean($id);
	
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/plan/upload');
		return true;
	}
	
	function verify($controller, $request) {
		$this->type = $this->_shift($this->type);
	
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/plan/verify');
		return true;
	}
	
	function verifyPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function replier($controller, $request) {
		$this->type = $this->_shift($this->type);
	
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/plan/replier');
		return true;
	}
	
	function replierPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$replier = $this->_input($request['replier']);
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET replier = '%s', content = concat(content, '%s'), modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $replier, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function finish($controller, $request) {
		$this->type = $this->_shift($this->type);
	
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/plan/finish');
		return true;
	}
	
	function finishPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$finish = $request['finish'];
		$reply = addslashes($request['reply']);
		
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET finish = '%s', reply = '%s', content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $finish, $reply, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function evaluation($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$email = array(
			'-' => '请选择',
			'1' => '是',
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);
	
		$controller->display($request, 'emp/plan/evaluation');
		return true;
	}
	
	function evaluationPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$evaluation = $request['evaluation'];
		$email = $request['email'];
	
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), evaluation = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $evaluation, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {
			$from = $_SESSION['id'];
	
			$to = $row['worker'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
				
			$subject = sprintf('任务信息 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'mold/plan/body_evaluation');
			// debug($body);
				
			$this->_notice($from, $to, $subject, $body, 1);
		}
	
		// $this->_notice_9($id, $status);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->type = $this->_shift($this->type);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/plan/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['week'] = $this->week($row['date']);
		
		$row['files'] = $this->_files($row['id']);
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/plan/copy');
		return true;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);
	
	
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
				
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}
	
			$this->_delete3($id);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
	
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
	
		$emp = $_SESSION['id'];
	
		if ($row['emp'] == $emp) {
			return true;
		}
	
		if ($row['oper'] == $emp) {
			return true;
		}
	
		return false;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', strtotime('-1 month'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按工时',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'info/plan/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$num_s = $request['num_s'];
		$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} elseif ($px == 'type') {
			$row['a'] = $this->type[$row['a']];
		} else {
	
			$row = $this->_name_t($row, $px);
		}
	
		return $row;
	}
	
	function ajax_b($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
	
		if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} elseif ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('15')) {
			$ret['status'] = 1;
		} elseif ($this->_is_admin()) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
	
		echo json_encode($ret);
	}
	
	function ajax_replier($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
	
		if ($this->_ajax_x($id, 'replier')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
	
		echo json_encode($ret);
	}

	function z($controller, $request) {

		logdate($controller, $request);
		return true;
	}
}
?>